package com.example.time_clock;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView textView;
    private EditText editText_time;
    private TextView textView2;
    private Button button_start;
    private TextView tv_show;
    int time;


    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            System.out.println("接收时间");
            tv_show.setText("计时时间："+msg.arg1);
            if(msg.arg1==time){
                Toast.makeText(MainActivity.this,"计时结束",Toast.LENGTH_LONG).show();
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        textView = (TextView) findViewById(R.id.textView);
        editText_time = (EditText) findViewById(R.id.editText_time);
        textView2 = (TextView) findViewById(R.id.textView2);
        button_start = (Button) findViewById(R.id.button_start);
        tv_show = (TextView) findViewById(R.id.tv_show);

        button_start.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button_start:
                time=Integer.valueOf(editText_time.getText().toString().trim());
                System.out.println("time；"+time);
                tv_show.setText("计时时间："+"0 S");
                Thread thread=new Thread(){
                    @Override
                    public void run() {
                        System.out.println("run");
                        int i;
                        for(i=1;i<=time;i++){
                            try{
                                Thread.sleep(1000);
                                System.out.println("发送时间");
                            }catch (Exception e){
                            }
                            Message message=handler.obtainMessage();
                            message.arg1=i;
                            handler.sendMessage(message);
                        }
                        super.run();
                    }
                };
                thread.start();
                break;
        }
    }

    private void submit() {
        // validate
        String time = editText_time.getText().toString().trim();
        if (TextUtils.isEmpty(time)) {
            Toast.makeText(this, "time不能为空", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO validate success, do something


    }
}
